﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace RecruitmentProcessSystem
{
    public partial class frmApplicantManager : Form
    {
        String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection conn;
        SqlCommand cmd, cmd_add, cmd_edit, cmd_del;
        SqlDataAdapter da;
        DataSet ds,ds_cbx;
        String back = "no";
        public frmApplicantManager(String back)
        {
            InitializeComponent();
            this.back = back;
        }
        public frmApplicantManager()
        {
            InitializeComponent();
        }

        private void frmApplicantManager_Load(object sender, EventArgs e)
        {
            btnEdit.Enabled = false;
            btnDel.Enabled = false;

            ds = new DataSet();
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd = new SqlCommand("Select * from Applicant", conn);
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Applicant");
            dataGridView1.DataSource = ds.Tables[0];
            conn.Close();

            //load combobox
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd = new SqlCommand("Select vname from Vacancy", conn);
            da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            ds_cbx = new DataSet();
            da.Fill(ds_cbx, "Vacancy Name");

            cbxVacancy.DataSource = ds_cbx.Tables[0];
            cbxVacancy.ValueMember = ds_cbx.Tables[0].Columns[0].ToString();
            cbxVacancy.DisplayMember = ds_cbx.Tables[0].Columns[0].ToString();
            da.Dispose();
            conn.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (back.Equals("yes")) {
                this.Dispose();
                this.Close();
                frmCreateInterview frm = new frmCreateInterview();
                frm.ShowDialog();
            }
            this.Close();
        }

        private void btnCreat_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "")
            {
                MessageBox.Show("Nhập tên ứng viên!");
            }
            else if (txtProfile.Text == "")
            {
                MessageBox.Show("Nhập lý lịch!");
            }
            else
            {
                conn = new SqlConnection(conStr);
                conn.Open();
                cmd_add = new SqlCommand("insert into Applicant(aname,aprofile,vname) values('" + txtName.Text + "','" + txtProfile.Text + "','" + cbxVacancy.SelectedValue + "')", conn);
                cmd_add.ExecuteNonQuery();
                conn.Close();
                this.frmApplicantManager_Load(sender, e);
                MessageBox.Show("Creat new successfully!");
            }
        }
        String id_tmp;
        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                btnEdit.Enabled = true;
                btnDel.Enabled = true;

                DataGridViewRow row = new DataGridViewRow();
                row = dataGridView1.Rows[e.RowIndex];
                id_tmp = row.Cells[0].Value.ToString();
                txtName.Text = row.Cells[1].Value.ToString();
                txtProfile.Text = row.Cells[2].Value.ToString();
                cbxVacancy.SelectedValue = row.Cells[3].Value.ToString();
            }
            catch { }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            conn = new SqlConnection(conStr);
            conn.Open();
            cmd_edit = new SqlCommand("update Applicant set aname = '" + txtName.Text + "',aprofile = '" + txtProfile.Text + "',vname = '" + cbxVacancy.SelectedValue + "' where appid = " + id_tmp, conn);
            cmd_edit.ExecuteNonQuery();
            conn.Close();
            this.frmApplicantManager_Load(sender, e);
            MessageBox.Show("Update successfully!");
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Ban muon xoa khong?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                conn = new SqlConnection(conStr);
                conn.Open();
                cmd_del = new SqlCommand("Delete from Applicant where appid = '" + id_tmp + "'", conn);
                cmd_del.ExecuteNonQuery();
                conn.Close();
                this.frmApplicantManager_Load(sender, e);
                MessageBox.Show("Delete successfully!");
            }
        }
    }
}
